package com.example.wxf.myapplication;

import android.content.ContentProvider;
import android.database.Cursor;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.leakcanary.LeakCanary;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
{

	ListView myListview;
	ArrayList<String> data = new ArrayList<>();
	LayoutInflater mInflater;
	RelativeLayout TopBlock;
	RelativeLayout ll[] = new RelativeLayout[2];
	LinearLayout itemBar;
	public static boolean onInterceptTouchEvent = false;

	ArrayList<RelativeLayout> mListView = new ArrayList<>();

	ViewPager mViewPager;


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mInflater = LayoutInflater.from(this);


		LeakCanary.install(getApplication());


		TopBlock = (RelativeLayout)mInflater.inflate(R.layout.top_layout, null);
		mViewPager = (ViewPager)TopBlock.findViewById(R.id.viewpager);
		itemBar = (LinearLayout)findViewById(R.id.topitemsbar);
		itemBar.setVisibility(View.INVISIBLE);
		for(int i = 0; i < 100; i++)
		{
			data.add("第" + i + "项");
		}

		for(int i = 0; i < 2; i++)
		{
			ll[i] = (RelativeLayout)mInflater.inflate(R.layout.viewpager_items, null);
			ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
			ListView listView = (ListView)ll[i].findViewById(R.id.listview);
			listView.setAdapter(new ListAdapter());
			mListView.add(ll[i]);
		}

		mViewPager.setAdapter(new ViewPageradapter(mListView));

		myListview = (ListView)findViewById(R.id.myListview);


		MyAdapter adapter = new MyAdapter();
		myListview.setAdapter(adapter);
		myListview.setOnScrollListener(new AbsListView.OnScrollListener()
		{
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
			{
				try
				{
					if(firstVisibleItem == 0)
					{
						if(view.getChildAt(firstVisibleItem).getTop() <= -300)
						{onInterceptTouchEvent = false;
							itemBar.setVisibility(View.VISIBLE);

							Log.i("wxf", "<");
						}
						else
						{onInterceptTouchEvent = true;
							itemBar.setVisibility(View.INVISIBLE);

							Log.i("wxf", ">");
						}
					}
				}
				catch(Exception e)
				{

				}

			}
		});
	}

	class ViewPageradapter extends PagerAdapter
	{

		private ArrayList<RelativeLayout> mViewList;

		public ViewPageradapter(ArrayList<RelativeLayout> ViewList)
		{
			// TODO Auto-generated constructor stub
			mViewList = ViewList;
		}

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			if(mViewList != null)
			{
				return mViewList.size();
			}
			return 0;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1)
		{
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position)
		{
			// TODO Auto-generated method stub
			View view = mViewList.get(position);
			if(position == 0)
			{
				view.setBackgroundColor(0xffff0000);
			}
			else
			{
				view.setBackgroundColor(0xff00ff00);
			}
			container.addView(view);
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object)
		{
			View view = mViewList.get(position);
			container.removeView(view);
		}

	}


	class ListAdapter extends BaseAdapter
	{


		@Override
		public int getCount()
		{
			return data.size();
		}

		@Override
		public Object getItem(int position)
		{
			return data.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			convertView = mInflater.inflate(R.layout.items, null);
			TextView textView = (TextView)convertView.findViewById(R.id.text);
			textView.setText(data.get(position));
			return convertView;
		}
	}

	class MyAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return 1;
		}

		@Override
		public Object getItem(int position)
		{
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			convertView = TopBlock;
			return convertView;
		}
	}


}
